package com.doform.android.xianggucheng.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.bean.CategoryBean;

import java.util.List;

/**
 * Created by ph on 2017/5/31.
 */

public class ArtCateExpListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<CategoryBean.ListBean> data;
    private ExpandableListView mExpListView;

    public ArtCateExpListAdapter(Context context, List<CategoryBean.ListBean> data, ExpandableListView mExpListView) {
        this.context = context;
        this.data = data;
        this.mExpListView = mExpListView;
    }

    @Override
    public int getGroupCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return data.get(groupPosition).get_child() == null ? 0 : data.get(groupPosition).get_child().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return data.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return data.get(groupPosition).get_child().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        mExpListView.expandGroup(groupPosition);

        ParentViewHolder parentHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_expanablelistview_parent, parent, false);
            parentHolder = new ParentViewHolder(convertView);
            convertView.setTag(parentHolder);
        } else {

            parentHolder = (ParentViewHolder) convertView.getTag();
        }
        parentHolder.tv_name_parent.setText(data.get(groupPosition).getClsname());
        parentHolder.iv_delete_parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //删除一级分类条目
                if (mOnItemDelete != null) {
                    mOnItemDelete.onItemClick(data.get(groupPosition).getId(), data.get(groupPosition).getGuid());
                }
            }
        });
        /**
         * 由于ExpandableListView内部问题，在ExpandableListView控件中如果删除group条目后调用notifyDataSetChanged
         * 或notifyDataSetInvalidated,会在原有条目下,添加Groups中最后一个groupPosition内的所有childPosition条目,
         * 故用利用展开和隐藏会重绘条目的特性替代notify
         */
        if (groupPosition == data.size() - 1) {
            mExpListView.collapseGroup(groupPosition);
            mExpListView.expandGroup(groupPosition);
        }
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        ChildViewHolder childHolder = null;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_expanablelistview_child, parent, false);
            childHolder = new ChildViewHolder(convertView);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildViewHolder) convertView.getTag();
        }
        childHolder.tv_name_child.setText(data.get(groupPosition).get_child().get(childPosition).getClsname());
        childHolder.iv_delete_child.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemDelete != null) {
                    mOnItemDelete.onItemClick(data.get(groupPosition).get_child().get(childPosition).getId()
                            , data.get(groupPosition).get_child().get(childPosition).getGuid());
                }
            }
        });
        return convertView;
    }

    //  子项是否可选中，如果需要设置子项的点击事件，需要返回true
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    private class ParentViewHolder {
        private ImageView iv_pic_parent, iv_delete_parent;
        private TextView tv_name_parent;

        public ParentViewHolder(View view) {
            tv_name_parent = (TextView) view.findViewById(R.id.tv_name_parent);
            iv_delete_parent = (ImageView) view.findViewById(R.id.iv_delete_parent);
        }
    }

    private class ChildViewHolder {
        private ImageView iv_pic_child, iv_delete_child;
        private TextView tv_name_child;

        public ChildViewHolder(View view) {
            tv_name_child = (TextView) view.findViewById(R.id.tv_name_child);
            iv_delete_child = (ImageView) view.findViewById(R.id.iv_delete_child);
        }
    }


    protected OnItemDelete mOnItemDelete;

    public interface OnItemDelete {
        void onItemClick(String id, String guid);
    }

    public void setOnItemClickListener(OnItemDelete onItemDeleteListener) {
        this.mOnItemDelete = onItemDeleteListener;
    }
}
